#ifndef	__C_VIEWS_VIEW_H__
#define	__C_VIEWS_VIEW_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "visbsc.h"
#include "vislist.h"

typedef enum view_draw_reason
{
	vw_draw_reason_refresh = 1,
	vw_draw_reason_resize,
	vw_draw_reason_show,
}
view_draw_reason_e;

typedef enum view_event_type
{
	view_event_unknown = 0,

	/*
	 * this event will be sent when the view is moved
	 * data: view_ev_move_data_t
	 */
	view_ev_move_event,

	/*
	 * this event will be sent when the view
	 * size has been changed
	 * data: view_ev_resize_data_t
	 */
	view_ev_resize_event,

	/*
	 * this event will be sent when a view
	 * is created
	 * data: none
	 */
	view_ev_create_event,

	/*
	 * this event will be sent when a view
	 * is to be shown (but not rendered)
	 * data: none
	 */
	view_ev_show_event,
}
view_event_type_e;

#define view_notify_event(vi, ev, data) \
do { \
	if ((vi) && (vi)->desc && (vi)->desc->event_handler) \
		(vi)->desc->event_handler((ev), (vi), (data)); \
} while (0)

typedef struct guid
{
	unsigned long  data1;
	unsigned short data2;
	unsigned short data3;
	unsigned char  data4[8];
}
guid_t;

#define guidcmp(g1, g2)	memcmp(g1, g2, sizeof(guid_t))
typedef const char* viewid_t;

struct region;
struct view_desc;

// definition of view, which is a key
// structure of views manager
typedef struct view
{
	listnode_t	 ownerlist;
	listnode_t	 children;
	struct view* parent;

	int left, top;
	int width, height;
	uint flags;

	struct view_desc* desc;
	void* data;
}
view_t;

#define VW_FLG_SHOW			(1)

#define view_getshow(v)		((v) && ((v)->flags & VW_FLG_SHOW))
#define view_rectnull(v)	((v)->width <= 0 || (v)->height <= 0)

typedef struct view_desc
{
	viewid_t viewid;
	int	style;

	// view_draw_reason_e represents the reason of this drawing
	// view_t* represents the view who is going to handle
	// region-t* represents the region to be drawn
	void (*draw_handler)(view_draw_reason_e, view_t*, struct region*);

	// view event handler
	void (*event_handler)(view_event_type_e, view_t*, void*);
}
view_desc_t;

// create a view
VISMGR_EXPORT_API view_t* view_create(view_t* parent, view_desc_t* desc, void* data);

// set parent of a view
VISMGR_EXPORT_API int view_setparent(view_t* parent, view_t* vi);

// set show/hide of a view
VISMGR_EXPORT_API void view_setshow(view_t* vi, _bool bshow);

// move a view and change its size
VISMGR_EXPORT_API int view_setpos(view_t* vi, int left, int top, int width, int height);

// find a parents with equal classid
VISMGR_EXPORT_API view_t* view_find_parent_by_viewid(view_t* vi, viewid_t vid);

// get the delta pos from an existed ancestor
VISMGR_EXPORT_API int view_get_delta_pos(view_t* vi, view_t* ancestor, int* deltax, int* deltay);

VISMGR_EXPORT_API void view_execute_action(void);

#ifdef __cplusplus
} // extern "C"
#endif
#endif	// __C_VIEWS_VIEW_H__
/* EOF */

